package com.jake.spring.boot.webflux.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jake.spring.boot.webflux.mapper.SchoolMapper;
import com.jake.spring.boot.webflux.model.School;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SchoolService {

    @Autowired
    private SchoolMapper schoolMapper;

    public List<School> list() {
        return schoolMapper.selectList(null);
    }

    public School getById(Integer schoolId) {
        return schoolMapper.selectById(schoolId);
    }

    @Transactional(rollbackFor = Exception.class)
    public Integer insert(School school) {
        return schoolMapper.insert(school);
    }

    @Transactional(rollbackFor = Exception.class)
    public Integer updateRow(School school) {
        return schoolMapper.updateById(school);
    }

    @Transactional(rollbackFor = Exception.class)
    public Integer updateName(School school) {
        UpdateWrapper<School> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("school_name", school.getSchoolName());
        updateWrapper.eq("school_id", school.getSchoolId());
        return schoolMapper.update(school, updateWrapper);
    }
}
